package com.ruoyi.user.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.user.api.domain.UcenterMember;
import com.ruoyi.user.service.IUcenterMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 会员Controller
 *
 * @author isahino
 * @date 2023-09-21
 */
@RestController
@RequestMapping("/member")
public class UcenterMemberController extends BaseController {
    @Autowired
    private IUcenterMemberService ucenterMemberService;

    /**
     * 查询会员列表
     */
    @RequiresPermissions("user:member:list")
    @GetMapping("/list")
    public TableDataInfo list(UcenterMember ucenterMember) {
        startPage();
        List<UcenterMember> list = ucenterMemberService.selectUcenterMemberList(ucenterMember);
        return getDataTable(list);
    }

    /**
     * 导出会员列表
     */
    @RequiresPermissions("user:member:export")
    @Log(title = "会员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UcenterMember ucenterMember) {
        List<UcenterMember> list = ucenterMemberService.selectUcenterMemberList(ucenterMember);
        ExcelUtil<UcenterMember> util = new ExcelUtil<>(UcenterMember.class);
        util.exportExcel(response, list, "会员数据");
    }

    /**
     * 获取会员详细信息
     */
    @RequiresPermissions("user:member:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(ucenterMemberService.selectUcenterMemberById(id));
    }

    /**
     * 修改会员
     */
    @RequiresPermissions("user:member:edit")
    @Log(title = "会员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UcenterMember ucenterMember) {
        // 密码加密
        ucenterMember.setPassword(SecurityUtils.encryptPassword(ucenterMember.getPassword()));
        return toAjax(ucenterMemberService.updateUcenterMember(ucenterMember));
    }

}
